//
//  SexListViewController.m
//  你的温实初
//
//  Created by lanou on 16/7/15.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "SexListViewController.h"

@interface SexListViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webview;

@end

@implementation SexListViewController

- (UIWebView *)webview {
    if (!_webview) {
        _webview = [[UIWebView alloc] initWithFrame:self.view.frame];
        _webview.scrollView.bounces = NO;
        _webview.scrollView.bouncesZoom = NO;
        _webview.delegate = self;
    }
    return _webview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webview];
    [RequestManager fetchDataWithUrl:[NSString stringWithFormat:@"http://dxy.com/app/i/columns/article/single?id=%@", self.ID] Parameter:@{} Method:@"GET" Compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@", self.ID);
        NSString *str = bigDic[@"data"][@"items"][0][@"content"];
        NSArray *array = [str componentsSeparatedByString:@"丁香园"];
        [self.webview loadHTMLString:array[0] baseURL:nil];
    } updateUI:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
//    NSString *js2 = @"var s= document.getElementsByTagName("img");
//    for(var i = 0; i < s.length; i++){
//        s[i].remove();
//        
//    }"
    NSString *js2 = @"var s= document.getElementsByTagName('img'); \
    for(var i = 0); i < s.length; i++) {\
        s[i].remove(); \
    }";
    
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:js2];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
